package song.NO_21;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Lianbiao {
    private Node head;
    private String data = null;
    public Lianbiao(){
        this.head = new Node();
    }
    public Lianbiao(final String data){
        this();
        this.data = data;
    }

    public Node getHead(){
        return this.head;
    }

    public void lianbiao_append(final Node node){
        var move = this.head;
        while (move.next_return() != null) move = move.next_return();
        move.setNext(node);
        move.next_return().setNext(null);
    }
    public void lianbiao_generating(){
        /**
         * 链表构造函数
         */
        this.head = new Node();
        var paras_list = Arrays.asList(this.data.split("->"));
        for (int i = paras_list.size()-1; i >= 0; --i){
            var move = new Node(Integer.parseInt(paras_list.get(i)));
            move.setNext(this.head.next_return());
            this.head.setNext(move);
        }
    }

    public void lianbiao_print(){
        var point = this.head.next_return();
        while (point != null){
            System.out.println(point.num_return());
            point = point.next_return();
        }
    }

    public static void main(String[] args){
        String s1 = "1->2->4";
        var L = new Lianbiao(s1);
        L.lianbiao_generating();
        L.lianbiao_print();
        var n = new Node(3);
        L.lianbiao_append(n);
        L.lianbiao_print();
//        var c1 = L.getHead().next_return();
//        var c2 = c1.next_return();
//        var arr = new ArrayList<Node>(){
//            {
//                add(c2);
//                add(c1);
//            }
//        };
//        Collections.sort(arr, new Node());
//        System.out.println(arr.get(0).num_return() + " " + arr.get(1).num_return());
    }
}
